package com.taotao.openapi2.controller;

import com.baidu.fsg.uid.impl.CachedUidGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;

import java.util.ArrayList;
import java.util.List;


@Valid
@Slf4j
@Api(tags = "百度uid演示")
@Controller
@Mapping("/uid")
public class UidGeneratorController {

    @Inject
    private CachedUidGenerator cachedUidGenerator;

    @ApiOperation(value = "单个获取", notes = "")
    @Mapping(value = "/getOne", method = MethodType.GET)
    public Long getOne() {
        return cachedUidGenerator.getUID();
    }

    @ApiOperation(value = "批量获取", notes = "")
    @Mapping(value = "/getBatch", method = MethodType.GET)
    public List<Long> getBatch(@ApiParam("名字") @Param(value = "name", defaultValue = "10") Integer batch) {

        List<Long> ids = new ArrayList<>(batch);

        for (int i = 0; i < batch; i++) {
            ids.add(cachedUidGenerator.getUID());
        }

        return ids;


    }


}